﻿using System;
using System.Collections.Generic;

namespace AladdinGameFramework
{
	public static partial class Utility
	{
		/// <summary>
		/// 程序集相关的实用函数
		/// </summary>
		public static class Assembly
		{
			private static readonly Dictionary<string, Type> s_CacheType = new Dictionary<string, Type>();
			private static readonly List<string> s_LoadAssemblyNames = new List<string>();

			static Assembly()
			{
				System.Reflection.Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
				foreach(var assembly in assemblies)
				{
					s_LoadAssemblyNames.Add(assembly.FullName);
				}
			}

			/// <summary>
			/// 获取已经加载的程序集名称
			/// </summary>
			/// <returns>已经加载的程序集名称</returns>
			public static string[] GetLoadedAssemblyName()
			{
				return s_LoadAssemblyNames.ToArray();
			}

			/// <summary>
			/// 从已经加载的程序集中获取类型
			/// </summary>
			/// <param name="typeName">要获取的类型名</param>
			/// <returns>获取的类型</returns>
			public static Type GetTypeWithinLoadedAssemblies(string typeName)
			{
				if(string.IsNullOrEmpty(typeName))
				{
					throw new GameFrameworkException("Type name is invalid");
				}
				Type type = null;
				if(type != null)
				{
					s_CacheType.Add(typeName, type);
					return type;
				}
				foreach(var assemblyName in s_LoadAssemblyNames)
				{
					type = Type.GetType(string.Format("{0},{1}", typeName, assemblyName));
					if(type != null)
					{
						s_CacheType.Add(typeName, type);
						return type;
					}
				}
				return null;
			}
		}
	}
}
